package com.ky.serialport.sensor;

import com.ky.serialport.bean.ComBean;
import com.ky.serialport.utils.MyFunc;

/**
 * 无线电压传感器（±24v）
 */
public class DianYa24DataBean {

    public Long time = 0L;
    public int power = 0; //电量
    public int signal = 0; //信号
    public int index = -1; //传感器编号
    public int type = -1;
    public ComBean comBean;
    public Float[] dianYaArray = new Float[5]; //电压数据
    public float dainYaVel = 0; //平均电压

    public DianYa24DataBean() {

    }

    public DianYa24DataBean(ComBean comBean) {
        caculate(comBean.recData);
        time = comBean.recTime;
        this.comBean = comBean;
    }

    public DianYa24DataBean(byte[] recData) {
        caculate(recData);
    }

    public DianYa24DataBean caculate(ComBean comBean) {
        time = comBean.recTime;
        this.comBean = comBean;
        return caculate(comBean.recData);
    }

    public DianYa24DataBean caculate(byte[] buffer) {
        time = System.currentTimeMillis();
        signal = MyFunc.getSignal(buffer[31]); //测试信号
        power = MyFunc.getPower(buffer[29], buffer[30]);
        index = buffer[10];
        type = buffer[9];
        dianYaArray[0] = (float) MyFunc.twoByte2IntNoSign(buffer[15], buffer[16]);
        dianYaArray[1] = (float) MyFunc.twoByte2IntNoSign(buffer[18], buffer[19]);
        dianYaArray[2] = (float) MyFunc.twoByte2IntNoSign(buffer[21], buffer[22]);
        dianYaArray[3] = (float) MyFunc.twoByte2IntNoSign(buffer[24], buffer[25]);
        dianYaArray[4] = (float) MyFunc.twoByte2IntNoSign(buffer[27], buffer[28]);

        dianYaArray[0] = (buffer[14] & 0xff) == 1 ? (0 - dianYaArray[0]) : dianYaArray[0];
        dianYaArray[1] = (buffer[17] & 0xff) == 1 ? (0 - dianYaArray[1]) : dianYaArray[1];
        dianYaArray[2] = (buffer[20] & 0xff) == 1 ? (0 - dianYaArray[2]) : dianYaArray[2];
        dianYaArray[3] = (buffer[23] & 0xff) == 1 ? (0 - dianYaArray[3]) : dianYaArray[3];
        dianYaArray[4] = (buffer[26] & 0xff) == 1 ? (0 - dianYaArray[4]) : dianYaArray[4];

        dainYaVel = (dianYaArray[0] + dianYaArray[1] + dianYaArray[2] + dianYaArray[3] + dianYaArray[4]) / 5f;
        return this;
    }
}
